﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Formatting;


namespace BasicallyNot.CodeTransformers
{

	internal enum StyleOffset
	{
		Foreground = 0,
		Background = 3,
	}



	internal class ViewFormatManager
	{

		internal IEditorFormatMap Formats { get; set; }

		private static string[] _propertyStyles = {
																							"ForegroundBrush",
																							"ForegroundColor",
																							"Foreground",
																							"BackgroundBrush",
																							"BackgroundColor",
																							"Background",
																							};

		private enum StyleIndex
		{
			Brush = 0,
			Color = 1,
			Fallback = 2,
		}



		private static string GetStyleString( StyleOffset style, StyleIndex type )
		{
			return _propertyStyles[ (int) style + (int) type ];
		}



		internal Brush GetProperty( StyleOffset style, string name )
		{
			ResourceDictionary properties = Formats.GetProperties( name );
			if ( properties == null )
				return null;

			if ( properties.Contains( GetStyleString( style, StyleIndex.Brush ) ) )
				return (Brush) properties[ GetStyleString( style, StyleIndex.Brush ) ];

			if ( properties.Contains( GetStyleString( style, StyleIndex.Color ) ) )
				return new SolidColorBrush( (Color) properties[ GetStyleString( style, StyleIndex.Color ) ] );

			if ( properties.Contains( GetStyleString( style, StyleIndex.Fallback ) ) )
				return (Brush) properties[ GetStyleString( style, StyleIndex.Fallback ) ];

			return null;
		}

	}

}
